<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        .body-style{
            width: 500px;
            margin:  0 auto;
        }
        .box-flex{
            width: 46%;
            display: inline-block;
        }
        .cpn-style{
            margin: 0 2%;
            margin-top: 30px;
            border: 1px solid #c8c8c8;
            border-radius: 10px;
            overflow: hidden;
        }
        .cpn-img{
            width: 100%;
            text-align: center;
        }
        img{
            width: 200px;
            height: 200px;
        }
        .pirce{
            font-size: 12px;
            text-align: center;
            color: black;
        }
        .text{
            color: red;
            text-align: center;
        }
        h2{
            text-align: center;
        }
    </style>
</head>
<body>
    <!-- vue实例 -->
    <div id="app" class="body-style">
        <h2>您当前点击的是 : {{ msg }} </h2>
        <div class="box-flex" v-for="item in list" :key="item.id">
            <my-cpn :listdata="item" @changeid="changeIdData"></my-cpn>
        </div>
        <!-- <div class="box-flex">
            <my-cpn :listdata="list[1]"></my-cpn>
        </div>
        <div class="box-flex">
            <my-cpn :listdata="list[2]"></my-cpn>
        </div>
        <div class="box-flex">
            <my-cpn :listdata="list[3]"></my-cpn>
        </div> -->
    </div>
    <!-- vue实例 -->

    <!-- 组件 -->
    <template id="myCpn">
        <div class="cpn-style" @click="clickCpn">
            <div class="cpn-img">
                <img :src="listdata.imgUrl" alt="">
            </div>
            <div class="text">
                {{ listdata.text }}
            </div>
            <div class="pirce">
                {{ listdata.price | newPrice }}
            </div>
        </div>
    </template>
    <!-- 组件 -->

    <script src="./js/vue.js"></script>
    <script>
        const app = new Vue({
            el:'#app',
            data(){
                return {
                    list:[
                        {
                            id:1,
                            name:'Redmi Note 9',
                            imgUrl:"./APP图片/4a05330038898964f80945746ebeec5b.png",
                            text:"Redmi Note 9",
                            price:999
                        },
                        {
                            id:2,
                            name:'Redmi Note 9 Pro',
                            imgUrl:"./APP图片/6dca45286d9d76d1f864a8ecf77f5600.jpg",
                            text:"Redmi Note 9 Pro",
                            price:1399
                        },
                        {
                            id:3,
                            name:'Xiaomi Ultra',
                            imgUrl:"./APP图片/7f5fab059643521e41406131379c7b4a.jpg",
                            text:"Xiaomi Ultra",
                            price:3999
                        },
                        {
                            id:4,
                            name:'MIX FOLD折叠屏手机',
                            imgUrl:"./APP图片/94b22bbff9fa84440a0aed48f9f63154.jpg",
                            text:"MIX FOLD折叠屏手机",
                            price:6999
                        },
                    ],
                    msg:null
                }
            },
            methods:{
                changeIdData(val){                  
                    this.msg = val;                   
                }
            },
            components:{
                'my-cpn':{
                    template:'#myCpn',
                    props:['listdata'],
                    data(){
                        return {

                        }
                    },
                    methods:{
                        clickCpn(){
                            this.$emit('changeid',this.listdata.name)
                        }
                    },
                    filters:{
                        newPrice(val){
                            return '¥' + val.toFixed(2)
                        }
                    }
                }
            }
        })
    </script>
</body>
</html>